﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using App.Models;
using App.Models.Pager;
using App.Views.ViewModels;

namespace App.Controllers
{
    public class VehicleInfoController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult List(int? page, int? rows, string order)
        {
            dbe.ContextOptions.ProxyCreationEnabled = false;
            dbe.ContextOptions.LazyLoadingEnabled = false;
            PagedList<tb_VehicleInfo> list = null;
            if ("asc" == order)
            {
                list = dbe.tb_VehicleInfo.OrderBy(m => m.VehicleNO)
                                .ToPagedList(page ?? 1, rows ?? 10);
            }
            else
            {
                list = dbe.tb_VehicleInfo.OrderByDescending(m => m.VehicleNO)
                                .ToPagedList(page ?? 1, rows ?? 10);
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Create(VehicleInfoViewModel  model)
        {
            try
            {
                var vh = new tb_VehicleInfo{
                    VehicleNO = model.VehicleNO,
                    VehicleType = model.VehicleType,
                    BuyTime = model.BuyTime,
                    Remark = model.Remark                        
                };                    
                dbe.AddTotb_VehicleInfo(vh);
                dbe.SaveChanges();
                var res = new { success = true };
                return Json(res, JsonRequestBehavior.AllowGet);                                
            }
            catch
            {
                var res = new { success = false, msg = "保存出错！" };
                return Json(res, JsonRequestBehavior.AllowGet);
            }
        }
                
        [HttpPost]
        public ActionResult Edit(string id, VehicleInfoViewModel  model)
        {
            try
            {
                id = model.VehicleNO;
                var vh = dbe.tb_VehicleInfo.FirstOrDefault(m => m.VehicleNO == id);
                vh.VehicleType = model.VehicleType;
                vh.BuyTime = model.BuyTime;
                vh.Remark = model.Remark;
                dbe.SaveChanges();
                var res = new { success = true };
                return Json(res, JsonRequestBehavior.AllowGet);     

            }
            catch
            {
                var res = new { success = false, msg = "保存出错！" };
                return Json(res, JsonRequestBehavior.AllowGet);
            }
        }
         
        
        public ActionResult Delete(string ids)
        {
            try
            {
                string[] idList = ids.Split('&');
                foreach (var id in idList) {
                    var obj = dbe.tb_VehicleInfo.FirstOrDefault(m => m.VehicleNO == id);
                    dbe.tb_VehicleInfo.DeleteObject(obj);
                }
                
                dbe.SaveChanges();
                var res = new { success = true };
                return Json(res, JsonRequestBehavior.AllowGet);
            }
            catch {
                var res = new { success = false,msg="删除出错！" };
                return Json(res,JsonRequestBehavior.AllowGet);
            }
        }
    }
}
